<?php
/**
 * 创建表
 * 其实可以在控制台来创建。就像mysql一样，没必要用代码来创建
 * author: JiangJun
 * CreateTime:2021/7/25 20:24
 */

require '../vendor/autoload.php';
use Aws\DynamoDb\Exception\DynamoDbException;



$sdk = new Aws\Sdk([
    'region'   => 'us-west-2',
    'version'  => 'latest',
    'credentials' => [
        'key' => 'AKIA4SBKFCJAXUOUXUN2',
        'secret' => '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',
    ],
]);

// 注意，上面的credentials。也可以不在代码里显示的配置密钥。而是事先将密钥配置在Linux的环境变量中，使用以下命令
/**
 *    export AWS_ACCESS_KEY_ID=AKIA4SBKFCJAXUOUXUN2
 *    export AWS_SECRET_ACCESS_KEY=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
 */


//获取DynamoDb的客户端对像
$dynamodb = $sdk->createDynamoDb();

$params = [
    'TableName' => 'Movies',//表名，必须
    'KeySchema' => [//必须，定义主键，这里是复合主键。
        [
            'AttributeName' => 'year',
            'KeyType' => 'HASH'  //Partition key
        ],
        [
            'AttributeName' => 'title',
            'KeyType' => 'RANGE'  //Sort key
        ]
    ],
    'AttributeDefinitions' => [//必须，定义主键的数据类型
        [
            'AttributeName' => 'year',
            'AttributeType' => 'N'//数字类型
        ],
        [
            'AttributeName' => 'title',
            'AttributeType' => 'S'//字符串类型
        ],

    ],
    'ProvisionedThroughput' => [//配置预置吞吐量
        'ReadCapacityUnits' => 1,
        'WriteCapacityUnits' => 1
    ]
];

try {
    //发送请求
    $result = $dynamodb->createTable($params);
    echo 'Created table.  Status: ' .
        $result['TableDescription']['TableStatus'] ."\n";

} catch (DynamoDbException $e) {
    echo "Unable to create table:\n";
    echo $e->getMessage() . "\n";
}